package com.mes.device.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.common.util.Constant;
import com.mes.device.mapper.UpkeepTypeMapper;
import com.mes.device.server.UpkeepTypeServer;
import com.mes.pojo.device.UpkeepType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 张喆
 * @Company
 * @Description
 * @Time 2024/10/18 16:26
 */
@Service
public class UpkeepTypeServerImpl implements UpkeepTypeServer {
    @Autowired
    private UpkeepTypeMapper upkeepTypeMapper;


    @Override
    public MESResult queryAll(UpkeepType upkeepType, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<UpkeepType> upkeepTypes = upkeepTypeMapper.selectAll(upkeepType);
        PageInfo<UpkeepType> upkeepTypePageInfo = new PageInfo<>(upkeepTypes);
        return MESResult.success(upkeepTypePageInfo);
    }

    @Override
    public MESResult addUpkeepType(UpkeepType upkeepType) {
        UpkeepType same = upkeepTypeMapper.selectByName(upkeepType.getUpkeepTypeName());
        if (same != null){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        String maxTypeId = upkeepTypeMapper.selectMaxId();
        String[] split = maxTypeId.split("_");
        int num = Integer.parseInt(split[1]);
        num = num + 1;
        String strNum = null;
        if (num > 0 && num < 10){
            strNum = "0000" + num;
        }else if (num < 100){
            strNum = "000" + num;
        }else if (num < 1000){
            strNum = "00" + num;
        }else if (num < 10000){
            strNum = "0" + num;
        }else {
            strNum = num + "";
        }
        upkeepType.setUpkeepTypeId(split[0] + "_" + strNum);
        return MESResult.success(upkeepTypeMapper.insertUpkeepType(upkeepType));
    }

    @Override
    public MESResult queryById(String upkeepTypeId) {
        return MESResult.success(upkeepTypeMapper.selectById(upkeepTypeId));
    }

    @Override
    public MESResult updateUpkeepTypeType(UpkeepType upkeepType) {
        UpkeepType same = upkeepTypeMapper.selectByName(upkeepType.getUpkeepTypeName());
        if (same != null && !same.getUpkeepTypeId().equals(upkeepType.getUpkeepTypeId())){
            return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
        }
        return MESResult.success(upkeepTypeMapper.updateUpKeepType(upkeepType));
    }

    @Override
    public MESResult deleteUpkeepTypeType(String upkeepTypeId) {
        UpkeepType upkeepType = upkeepTypeMapper.selectById(upkeepTypeId);
        upkeepType.setUpkeepTypeStatus(99);
        return MESResult.success(upkeepTypeMapper.updateUpKeepType(upkeepType));
    }
}
